﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.IO;
using System.Globalization;
/*
Copyright 2011 Mirosław M. Mączka

This file is part of MobiLang.

    MobiLang is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace MobiLang
{
    public sealed class Utils
    {
        public static string CleanFileName(string s)
        {
            char[] invs = Path.GetInvalidFileNameChars();
            //
            s = s.Replace('<', 'ź');
            s = s.Replace('>', '╗');
            //
            s = s.Replace('"', '\'');
            //
            foreach (char c in invs)
            {
                s = s.Replace(c, '_');
            }
            // bo ja wiem...
            s = s.Replace('_', ' ');
            s.Replace("  ", " ");
            s.Replace("  ", " ");
            s.Replace("  ", " ");
            //
            s = RemoveAccents(s);
            s = RemoveQuotes(s);
            //
            s = s.Trim();
            //
            return s;
        }
        //--------------------------------------
        public static string RemoveAccents(string s)
        {
            string so = "";
            //
            s = s.Normalize(NormalizationForm.FormD);

            for (int ich = 0; ich < s.Length; ich++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(s[ich]);
                if (uc != UnicodeCategory.NonSpacingMark)
                {
                    so += s[ich];
                }
            }
            //
            return so;
        }
        //--------------------------------------
        public static string RemoveQuotes(string s)
        {
            if (s.StartsWith("\"") && s.EndsWith("\""))
            {
                return s.Substring(1, s.Length - 2);
            }
            //
            return s;
        }

    }
}
